/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver;

import java.io.Serializable;
import java.util.Date;
import java.util.logging.Level;

public class HttpSessionObserver extends Observer implements Serializable {

	 private static final long serialVersionUID = 40L;

	 private final String project;

	 private Date date;

	 private transient long startTimeNanos;

	 private transient long stopTimeNanos;

	 private long timeNanos;

	 private boolean closed;

	 //Test
	 HttpSessionObserver(Date date, String context, String project, long timeNanos) {
			super(context);
			this.project = project;
			this.date = date;
			this.timeNanos = timeNanos;

			this.closed = (this.timeNanos > 0) ? true : false;
	 }

	 HttpSessionObserver(String project, String sessionId) {
			super(JeeObserverContext.getInstance().getContext());
			this.project = (project == null) ? "" : project;
	 }

	 void start() {
			if (JeeObserverContext.getInstance() != null) {

				 this.setRunning(true);

				 this.date = new Date();

				 this.closed = false;

				 this.startTimeNanos = System.nanoTime();
				 this.stopTimeNanos = 0;

				 JeeObserverContext.getInstance().addObserver(this);

			} else {
				 JeeObserverContext.logger.log(Level.FINE, "JeeObserver context not exists.");
			}
	 }

	 void stop() {
			if (JeeObserverContext.getInstance() != null) {
				 if (this.isRunning()) {
						this.stopTimeNanos = System.nanoTime();

						this.timeNanos = this.startTimeNanos - this.stopTimeNanos;

						JeeObserverContext.getInstance().addObserver(this);

						this.setRunning(false);
				 }
			} else {
				 JeeObserverContext.logger.log(Level.FINE, "JeeObserver context not exists.");
			}
	 }

	 public Date getDate() {
			return date;
	 }

	 public boolean isClosed() {
			return closed;
	 }

	 public long getTimeNanos() {
			return timeNanos;
	 }

	 public String getProject() {
			return this.project;
	 }
}
